package com.warframe.message;

import com.sobte.cqp.jcq.entity.CoolQ;
import com.tools.HibernateUtils;
import com.tools.StringContent;
import com.warframe.entity.database.RegexContentEO;
import com.warframe.entity.message.Message;
import com.warframe.entity.regex.RegexContentMO;
import com.warframe.robot;
import org.hibernate.Criteria;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class Interpreter implements Runnable {
    //单例
    private static Interpreter interpreter;

    private Interpreter() {
    }

    private static class SingletonHolder {
        private final static Interpreter instance = new Interpreter();
    }

    public static Interpreter getInstance() {
        return Interpreter.SingletonHolder.instance;
    }

    private static InterMsgQueue interMsgQueue = InterMsgQueue.getInstance();
    private static Boolean quit = false;
    private static List<RegexContentMO> regexContentMOList = new ArrayList();
    private static String patternNumber = "\\d";
    private static CoolQ CQ;

    public static CoolQ getCQ() {
        return CQ;
    }

    public static void setCQ(CoolQ CQ) {
        Interpreter.CQ = CQ;
    }

    private static void regexInit() {

        if (regexContentMOList.size() > 0) {
            regexContentMOList.clear();
        }
        Session session = HibernateUtils.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(RegexContentEO.class);
        List<RegexContentEO> regexContentEOList = criteria.list();
        for (RegexContentEO eachRegexContentEO : regexContentEOList) {
            RegexContentMO eachRegexContentMO = new RegexContentMO(
                    eachRegexContentEO.getId(),
                    eachRegexContentEO.getRegexText(),
                    eachRegexContentEO.getType()
            );
            regexContentMOList.add(eachRegexContentMO);
        }
        session.close();
    }

    public void init() {
        regexInit();//初始化匹配列表
    }

    public void quit() {
        quit = true;
    }

    @Override
    public void run() {
        this.init();
        System.out.println("Interpreter Init Finished");
        CQ.logInfo("WarframeRobot", "Interpreter Init Finished");
        while (!quit) {
            if (!interMsgQueue.isEmpty()) {
                Message message = interMsgQueue.poll();
                System.out.println(message.getMsg());
                for (RegexContentMO eachRegexContentMO : regexContentMOList) {
                    if (eachRegexContentMO.getRegexText().matcher(message.getMsg()).matches()) {
                        System.out.println(eachRegexContentMO.getRegexText());
                        message.setSign(eachRegexContentMO.getType().toString());
                        message.setMsg(message.getMsg().substring(message.getMsg().indexOf(" ") + 1));
                        Executor.getInstance().pushMsg(message);
                        break;
                    } else if (null == message.getGroupId()//群号为空
                            && Pattern.compile(patternNumber).matcher(message.getMsg()).matches()) {//回复数字
                        Map<Integer, Message> layer = StringContent.MESSAGELAYER.get(message.getFromQQ());
                        if (null!= layer && !layer.isEmpty()) {
                            Message layerMessage = layer.get(Integer.valueOf(message.getMsg()));
                            if (null != layerMessage) {
                                Executor.getInstance().pushMsg(layerMessage);
                                StringContent.MESSAGELAYER.get(message.getFromQQ()).remove(Integer.valueOf(message.getMsg()));
                                if (StringContent.MESSAGELAYER.get(message.getFromQQ()).size() == 0) {
                                    StringContent.MESSAGELAYER.remove(message.getFromQQ());
                                }
                                break;
                            } else {
                                Executor.getInstance().pushMsg(new Message(message.getFromQQ(), "您未使用过递进查询，或查询结果已发送。", StringContent.SENDPERSON));
                                break;
                            }
                        }
                        //System.out.println(message);
                    }
                }
            } else {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void pushMsg(Message msg) {
        interMsgQueue.add(msg);
    }

}
